#include "fastbufferhandler.h"
#include "fastbuffermanager.h"
#include <qmath.h>

FastBufferHandler::FastBufferHandler(int id, FastBufferManager *manager)
    : m_id(id), m_manager(manager), m_maxTimeCount(0), m_minTimeCount(10000)
{
}

void FastBufferHandler::waitDataReady()
{
    m_manager->markBufferReady(m_id, true);
    m_manager->waitBufferReady(1);
}

void FastBufferHandler::waitDataInit()
{
    m_manager->markBufferReady(m_id, false);
    m_manager->waitBufferReady(0);
}

void FastBufferHandler::sendData()
{
    m_manager->markBuffer(m_id, true);
}

void FastBufferHandler::recvData()
{
    m_manager->readyToRead(m_id);
}

void FastBufferHandler::readFinish()
{
    m_manager->markBuffer(m_id, false);
}

void FastBufferHandler::recordTimeout(long long ns)
{
    m_timeCount.push_back(ns);
    m_maxTimeCount = qMax(ns, m_maxTimeCount);
    m_minTimeCount = qMin(ns, m_minTimeCount);
    // m_manager->recordTimeout(ns);
}

int FastBufferHandler::id() const
{
    return m_id;
}

QVector<double> &FastBufferHandler::fastBuffer()
{
    return m_manager->fastBuffer(m_id);
}
